/***************************************************************************
 *
 * Copyright (c) 2013 Baidu, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************/
// Author: Wen Xiang <bigflow-opensource@baidu.com>
//
// Main execution class of flume runtime.

#ifndef FLUME_RUNTIME_TASK_H_
#define FLUME_RUNTIME_TASK_H_

#include <vector>

#include "boost/scoped_ptr.hpp"
#include "boost/ptr_container/ptr_vector.hpp"
#include "toft/base/string/string_piece.h"

#include "flume/proto/physical_plan.pb.h"
#include "flume/runtime/executor.h"
#include "flume/runtime/executor_factory.h"

namespace baidu {
namespace flume {
namespace runtime {

class Task {
public:
    Task() {}
    ~Task() {}

    void Initialize(const PbExecutor& message, ExecutorFactory* factory);

    void Run(const toft::StringPiece& info);

    Executor* Root() { return m_root; };

private:
    bool ChildIsInBatchMode(const PbExecutor& message);

    // for batch
    Executor* CreateExecutor(
            const PbExecutor& self,
            uint32_t scope_level,
            ExecutorFactory* factory);

    void RunAsBatch(const toft::StringPiece& info);

private:
    PbExecutor m_message;
    // for batch
    boost::ptr_vector<Executor> m_executors;
    Executor* m_root;
};

}  // namespace runtime
}  // namespace flume
}  // namespace baidu

#endif  // FLUME_RUNTIME_TASK_H_
